package org.gikolet.guice.db;

import java.lang.reflect.Proxy;

import roboguice.inject.ContextScoped;

import com.google.inject.Inject;
import com.google.inject.Provider;

@ContextScoped
public class TransactionProvider<T> implements Provider<T> {
	@Inject
	@TransactionObject
	private T transactionObject;

	@Inject
	private TransactionManager transactionManager;

	@SuppressWarnings("unchecked")
	public T get() {
		return (T) Proxy.newProxyInstance(this.transactionObject.getClass()
				.getClassLoader(), this.transactionObject.getClass()
				.getInterfaces(), new TransactionProxy<T>(this.transactionManager,
				this.transactionObject));
	}
}
